package com.hyd.quartz.task;

import com.hyd.manage.domain.Abnormal;
import com.hyd.manage.domain.Medicines;
import com.hyd.manage.domain.StockAlert;
import com.hyd.manage.service.IAbnormalService;
import com.hyd.manage.service.IMedicinesService;
import com.hyd.manage.service.IStockAlertService;
import javazoom.jl.player.Player;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class MyTask {

    @Autowired
    private IMedicinesService medicinesService;
    @Autowired
    private IAbnormalService abnormalService;
    @Autowired
    private IStockAlertService stockAlertService;

    /**
     * 检测药品过期时间
     *
     */
    public void checkMedicationTime() {
        List<Medicines> lists = medicinesService.selectMedicinesList(null);
        log.info("查询到" + lists.size() + "条数据");
        for (Medicines list : lists) {
            // 判断是否过期
            if (list.getExpirationDate().getTime() < new Date().getTime()) {
                log.info("药品" + list.getMedicineName() + "过期了");
                // 将过期的药品放入“异常药品表中”
                Abnormal abnormal = new Abnormal();
                BeanUtils.copyProperties(list, abnormal);
                log.info("异常药品：" + abnormal);
                abnormal.setAbnormalReason("1");
                abnormal.setQuantity(list.getStockQuantity());
                abnormal.setAbnormalDate(new Date());
                abnormal.setStatus("0");
                abnormalService.insertAbnormal(abnormal);
                // 从库中删除该药品
                medicinesService.deleteMedicinesById(list.getId());
            }
        }
    }

    /**
     * 检测库存预警
     *
     */
    public void checkStockAlert() {
        List<StockAlert> lists = stockAlertService.selectStockAlertList(null);
        for (StockAlert list : lists) {
            // 通过药品编号和药品批次号查询库存
            Medicines medicines = medicinesService.selectMedicineByCodeAndNumber(list.getMedicineCode(), list.getBatchNumber());
            // 判断库存是否小于等于最小预警值
            if (medicines.getStockQuantity() <= list.getMinQuantity()) {
                // 将库存小于等于最小预警值的药品放入“异常药品表中”
                Abnormal abnormal = new Abnormal();
                BeanUtils.copyProperties(medicines, abnormal);
                abnormal.setAbnormalReason("5");
                abnormal.setQuantity(medicines.getStockQuantity());
                abnormal.setAbnormalDate(new Date());
                abnormal.setStatus("0");
                abnormalService.insertAbnormal(abnormal);
            }
            // 判断库存是否大于等于最大预警值
            else if (medicines.getStockQuantity() >= list.getMaxQuantity()) {
                // 将库存大于等于最大预警值的药品放入“异常药品表中”
                Abnormal abnormal = new Abnormal();
                BeanUtils.copyProperties(medicines, abnormal);
                abnormal.setAbnormalReason("4");
                abnormal.setQuantity(medicines.getStockQuantity());
                abnormal.setAbnormalDate(new Date());
                abnormal.setStatus("0");
                abnormalService.insertAbnormal(abnormal);
            }
        }
    }

    public void checkAbnormal() throws Exception {
        List<Abnormal> lists = abnormalService.selectAbnormalList(null);
        for (Abnormal list : lists) {
            // 查看是否有待处理的异常药品
            if (list.getStatus().equals("0")) {
                // 使用类加载器获取资源文件的输入流
                InputStream inputStream = getClass().getClassLoader().getResourceAsStream("voice\\voice.mp3");
                if (inputStream == null) {
                    System.err.println("音频文件未找到：voice/voice.mp3");
                    return;
                }
                try (BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream)) {
                    // 创建Player对象并播放音频
                    Player player = new Player(bufferedInputStream);
                    player.play();
                } catch (Exception e) {
                    System.err.println("播放音频时出错：" + e.getMessage());
                    e.printStackTrace();
                }
                /*File file=new File("E:\\voice\\voice.mp3");
                FileInputStream stream=new FileInputStream(file);
                Player player=new Player(stream);
                player.play();*/
                //向前端发送消息

                break;
            }
        }
    }

    public void showTime() {
        System.out.println("定时任务开始执行：" + new Date());
    }
}
